import os
import sys


def get_current_path(path):
    path, name = os.path.split(path)
    return path
    pass


def split_path(path, split_str='/'):
    return remove(path.split(split_str))
    pass


def remove(l, value=''):
    if not isinstance(l, list):
        return l
    if empty(l):
        return l
    rs = []
    for i in range(len(l)):
        v = l[i]
        if v != value:
            rs.append(v)
    return rs
    pass


def empty(d):
    if d is None or d == '' or d == 0:
        return True
        pass
    if isinstance(d, list) and len(d) == 0:
        return True
        pass
    if isinstance(d, dict) and len(d.keys()) == 0:
        return True
        pass
    return False
    pass


def get_module_name(file_path):
    path, name = os.path.split(file_path)
    return remove(split_path(path))[-1]
    pass


def get_module(file_path):
    module_name = get_module_name(file_path)
    if module_name not in sys.modules:
        raise Exception('找不到module')
    return sys.modules[module_name]
    pass


def get_dict(d, k, default_value=''):
    return d[k] if k in d else default_value
    pass


def get_spell(name, dictionary):
    result = ''
    for n in name:
        if n in dictionary:
            result += dictionary[n]
            pass
        else:
            # 暂时不处理
            pass
        pass
    return result
    pass


def get_abbreviation(name, dictionary):
    result = ''
    for n in name:
        if n in dictionary:
            result += dictionary[n][0]
            pass
        else:
            # 暂时不处理
            pass
        pass
    return result
    pass


def compare_str(str1, str2):
    """
    若 str2 是 str1 的一部分(忽略大小写)
    返回true
    否则返回false
    :param str1: string
    :param str2: string
    :return:
    """
    return str1.lower().find(str2.lower()) > -1
    pass


def init_item(**kwargs):
    item = {}
    if 'text' in kwargs:
        item['text'] = kwargs['text']
    if 'subtext' in kwargs:
        item['subtext'] = kwargs['subtext']
    if 'completion' in kwargs:
        item['completion'] = kwargs['completion']
    if 'actions' in kwargs:
        item['actions'] = kwargs['actions']
    return item
    pass
